package nl.hu.vakantievibes.client;

import nl.hu.vakantievibes.domain.Gebruiker;
import nl.hu.vakantievibes.ui.ShowReizen;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Systeem {

	private static Systeem sys = null;
	public Gebruiker ingelogdeGebruiker;
	
	private Systeem() { }
	
	public static Systeem get() {
		if(sys == null) {
			sys = new Systeem();
		}
		return sys;
	}
	
	public boolean loggedIn() {
		if(ingelogdeGebruiker == null) {
			return false;
		} else {
			return true;
		}
	}
	
	public boolean isAdmin() {
		if(ingelogdeGebruiker == null) {
			return false;
		} else if(ingelogdeGebruiker.getAdmin()) {
			return true;
		}
		return false;
	}
	
	public void logout() {
		this.ingelogdeGebruiker = null;
		showPage(new ShowReizen().createPage());
		createLinks();
		createNavigation();
	}
	
	public void setGebruiker(Gebruiker g) {
		ingelogdeGebruiker = g;
	}
	
	public Gebruiker getGebruiker() {
		return ingelogdeGebruiker;
	}
	
	public void createLinks() {
		RootPanel loginField = RootPanel.get("loginlinks");
		loginField.clear();
		
		if(!loggedIn()) {
			HorizontalPanel loginPanel = new HorizontalPanel();	
			//Register Link
			Anchor registerLink = new Anchor("Registreren");
			registerLink.setStyleName("loginLink");
			registerLink.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					History.newItem("register");
				}
			});
			loginPanel.add(registerLink);
			loginPanel.setCellWidth(registerLink, "100px");
			
			//Login Link
			Anchor loginLink = new Anchor("Inloggen");
			loginLink.setStyleName("loginLink");
			loginLink.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					History.newItem("login");
				}
			});
			loginPanel.add(loginLink);
			loginField.add(loginPanel);
		} else {
			//Logout Link
			Anchor logoutLink = new Anchor("asdasdjhabsdjbasjdhbajsbd");
			logoutLink.addStyleName("loginLink");
			logoutLink.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					logout();
				}
			});
			loginField.add(logoutLink);
		}
	}
	
	public void createNavigation() {
		RootPanel menu = RootPanel.get("menu");
		menu.clear();
		menu.add(new HTML("<li><a href=\"#allereizen\">Home</a></li>"));
		if(loggedIn()) {
			menu.add(new HTML("<li><a href=\"#mijnboekingen\">Mijn Boekingen</a></li>"));
			menu.add(new HTML("<li><a href=\"#mijncarpools\">Mijn Carpools</a></li>"));
		}
		if(isAdmin()) {
			menu.add(new HTML("<li><a href=\"#reistoevoegen\">Reis Toevoegen</a></li>"));
		}
	}

	public void showPage(Widget w) {
		RootPanel main = RootPanel.get("main");
		main.clear();
		main.add(w);
	}
}
